package log350.restaurant;

import java.text.DecimalFormat;
import java.util.ArrayList;

import Class.Commande;
import Class.SingletonCommandesEnCours;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

public class Rapports extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_rapports);
		// Show the Up button in the action bar
		getActionBar().setDisplayHomeAsUpEnabled(true);

		generateDailyReport();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_rapports, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			NavUtils.navigateUpFromSameTask(this);
			return true;
		case R.id.Menu:
			Intent intent = new Intent(this, MainActivity.class);
			startActivity(intent);
			return (true);
		}

		return super.onOptionsItemSelected(item);
	}

	private void generateDailyReport() {
		ArrayList<Commande> commandes = SingletonCommandesEnCours.getCourant().getListCommande();

		double nbCommandes = commandes.size();
		if (nbCommandes == 0)
			return;
		
		double nbItemsTotal = 0;
		double nbItemsMoyenCommande = 0;
		double coutMoyen = 0;
		double tpsTotal = 0;
		double tvqTotal = 0;
		double total = 0;

		for (int i = 0; i < nbCommandes; i++) {
			for (int i2 = 0; i2 < commandes.get(i).getItems().size(); i2++)
			{
				nbItemsTotal += commandes.get(i).getItems().get(i2).getQuantite();
			}
			tpsTotal += commandes.get(i).getTaxeTPS();
			tvqTotal += commandes.get(i).getTaxeTVQ();
			total += commandes.get(i).getTotal();
		}
		nbItemsMoyenCommande = nbItemsTotal / nbCommandes;
		coutMoyen = total / nbCommandes;

		DecimalFormat df = new DecimalFormat("#.##");
		
		int nbCommandesInt = (int) nbCommandes;
		int nbItemsMoyenCommandeInt = (int)nbItemsMoyenCommande;
		
		TextView txtNbCommandes = (TextView) findViewById(R.id.txtNbCommandes);
		txtNbCommandes.setText(String.valueOf(nbCommandesInt));

		TextView txtNbItemMoyenParCommande = (TextView) findViewById(R.id.txtNbItemMoyenParCommande);
		txtNbItemMoyenParCommande.setText(String.valueOf(nbItemsMoyenCommandeInt));

		TextView txtCoutMoyenFacture = (TextView) findViewById(R.id.txtCoutMoyenFacture);
		txtCoutMoyenFacture.setText(String.valueOf(df.format(coutMoyen)));

		TextView txtTPS = (TextView) findViewById(R.id.txtTPS);
		txtTPS.setText(String.valueOf(df.format(tpsTotal)));

		TextView txtTVQ = (TextView) findViewById(R.id.txtTVQ);
		txtTVQ.setText(String.valueOf(df.format(tvqTotal)));

		TextView txtTotal = (TextView) findViewById(R.id.txtTotal);
		txtTotal.setText(String.valueOf(df.format(total)));
	}
}
